import 'package:flutter/material.dart';
import 'BaseFragment.dart';
import 'NavigatorManger.dart';

///基类StatefulWidget
abstract class BaseActivity extends StatefulWidget {
  const BaseActivity({super.key});

/*@override
  BaseFulWidgetState createState() {
    return getState();
  }

  BaseFulWidgetState getState();*/
}

///基类State
abstract class BaseActivityState<T extends BaseActivity> extends State<T>
    with WidgetsBindingObserver {

  late String tag = "";
  final String _dTag = "BaseWidgetState_";
  bool _onResumed = false; //页面展示标记
  bool _onPause = false; //页面暂停标记

  //通过上下文获取page唯一标识
  String getClassName(BuildContext _buildContext) {
    if (_buildContext == null) {
      return _dTag;
    }
    String className = _buildContext.toString();
    if (className == null) {
      return _dTag;
    }
    className = className.substring(0, className.indexOf("(")).toString();
    return className;
  }

  BaseFragment createFragment(BaseFragment fragment,BaseFragmentState fragmentState) {
    return fragment;
  }

  @override
  void initState() {
    super.initState();
    print("${tag}initState\n");
    tag = getClassName(context);
    onCreate();
    //添加page唯一标识到数组
    NavigatorManger().addWidget(context);
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  void didChangeDependencies() {
    print("${tag}didChangeDependencies\n");
    super.didChangeDependencies();
  }

  @override
  Widget build(BuildContext context) {
    print("${tag}build\n");
    if (!_onResumed) {
      //初次加载 顶部page
      if (NavigatorManger().isTopPage(context)) {
        _onResumed = true;
        onResume();
      }
    }
    return initView(context);
  }

  @override
  void didUpdateWidget(T oldWidget) {
    print("${tag}didUpdateWidget\n");
    super.didUpdateWidget(oldWidget);
  }

  @override
  void reassemble() {
    print("${tag}reassemble\n");
    super.reassemble();
  }

  @override
  void deactivate() {
    print("${tag}deactivate\n");
    //仅次于顶部的page
    if (NavigatorManger().isSecondTop(context)) {
      if (!_onPause) {
        onPause();
        _onPause = true;
      } else {
        onResume();
        _onPause = false;
      }
      //顶部page
    } else if (NavigatorManger().isTopPage(context)) {
      if (!_onPause) {
        onPause();
      }
    }
    super.deactivate();
  }

  @override
  void dispose() {
    print("${tag}dispose\n");
    _onResumed = false;
    _onPause = false;
    //把改页面 从 页面列表中 去除
    NavigatorManger().removeWidget(context);
    WidgetsBinding.instance.removeObserver(this);
    onDestroy();
    super.dispose();
  }

  void onCreate() {
    print("${tag}onCreate\n");
  }

  void onResume() {
    print("${tag}onResume\n");
  }

  void onPause() {
    print("${tag}onPause\n");
  }

  void onDestroy() {
    print("${tag}onDestroy\n");
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    switch (state) {
      case AppLifecycleState.resumed:
        onResume();
        break;
      case AppLifecycleState.paused:
        onPause();
        break;
    }
  }

  ///主动关闭页面
  void finish(){
    onDestroy();
  }

  Widget initView(BuildContext context);
}
